package com.crm.system.service.impl.cluePoolManager;

import com.alibaba.fastjson.JSONObject;
import com.crm.common.util.CommonUtils;
import com.crm.model.entity.cluePoolManager.CluePoolManager;
import com.crm.model.entity.sys.UserAccount;
import com.crm.model.vo.cluePoolManager.CluePoolManagerPeopleVO;
import com.crm.service.cluePoolManager.CluePoolManagerDbService;
import com.crm.system.service.cluePoolManager.CluePoolManagerService;
import com.crm.system.service.cluePoolManager.CluePoolPublicService;
import com.crm.system.service.user.UserAccountService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author luojie
 * @date 2021/1/11 16:44
 */
@Service
public class CluePoolManagerServiceImpl implements CluePoolManagerService {

    @Autowired
    private CluePoolManagerDbService cluePoolManagerDbService;
    @Autowired
    private UserAccountService userAccountService;

    @Override
    public Boolean insertCluePoolManager(String poolId, List<JSONObject> peopleIdList) {
        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();
        peopleIdList.stream().forEach(peopleId ->{
            CluePoolManager cluePoolManager = cluePoolManagerDbService.selectCluePoolManagerByPoolIdAndPeopleId(poolId,peopleId.getString("id"));
            if(cluePoolManager==null){
                CluePoolManager cluePoolManagerAdd = new CluePoolManager();
                cluePoolManagerAdd.setCluePoolManagerId(CommonUtils.getGUID());
                cluePoolManagerAdd.setCluePoolId(poolId);
                cluePoolManagerAdd.setManagerPeopleId(peopleId.getString("id"));
                cluePoolManagerAdd.setCreatePeopleId(userAccount.getAccountId());
                cluePoolManagerAdd.setCreateTm(new Date());
                cluePoolManagerDbService.insertCluePoolManager(cluePoolManagerAdd);
            }
        });
        return true;
    }

    @Override
    public List<CluePoolManagerPeopleVO> selectCluePoolManagerPeopleList(String cluePoolId,String companyId) {
        //查询线索池管理的管理用户id的List
        List<CluePoolManager> cluePoolManagers = cluePoolManagerDbService.selectCluePoolManagerByPoolId(cluePoolId);

        List<UserAccount> userAccounts = userAccountService.selectUserAccountByCompanyId(companyId);
        List<CluePoolManagerPeopleVO> cluePoolManagerPeopleVOList = new ArrayList<>();
        userAccounts.stream().forEach(userAccount ->{
            CluePoolManagerPeopleVO cluePoolManagerPeopleVO = new CluePoolManagerPeopleVO();
            cluePoolManagerPeopleVO.setAccountId(userAccount.getAccountId());
            cluePoolManagerPeopleVO.setUserName(userAccount.getUserName());
            //判断管理用户id集合里面如果包含账号id设置为true
            Boolean result = false;
            for (CluePoolManager cluePoolManager : cluePoolManagers) {
                if(cluePoolManager.getManagerPeopleId().equals(userAccount.getAccountId())){
                    result = true;
                }
            }
            cluePoolManagerPeopleVO.setSelected(result);
/*           if(cluePoolManagers.stream().map(CluePoolManager::getCluePoolManagerId).anyMatch(managerId->managerId.equals(userAccount.getAccountId()))){
               cluePoolManagerPeopleVO.setSelected(true);
            }else {
               cluePoolManagerPeopleVO.setSelected(false);
           }*/
            cluePoolManagerPeopleVOList.add(cluePoolManagerPeopleVO);
        });
        return cluePoolManagerPeopleVOList;
    }

    @Override
    public Boolean deleteCluePoolManager(String poolId) {
        Boolean result = cluePoolManagerDbService.deleteCluePoolManager(poolId);
        return result;
    }

    @Override
    public Boolean insertCluePoolManagerByCreatePeople(String cluePoolId) {
        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();
        CluePoolManager cluePoolManager = new CluePoolManager();
        cluePoolManager.setCluePoolManagerId(CommonUtils.getGUID());
        cluePoolManager.setCluePoolId(cluePoolId);
        cluePoolManager.setManagerPeopleId(userAccount.getAccountId());
        cluePoolManager.setCreatePeopleId(userAccount.getAccountId());
        cluePoolManager.setCreateTm(new Date());
        Boolean result = cluePoolManagerDbService.insertCluePoolManager(cluePoolManager);
        return result;
    }
}
